package han.main;

import han.entity.FriendEntity;
import han.entity.GroupEntity;
import han.entity.MessageEntity;
import han.gui.MainFrame;
import han.message.*;
import han.parser.GroupParser;
import han.proof.FriendListProof;
import han.proof.GroupDetailInfo;
import han.proof.GroupWebsiteLogin;
import han.proof.SmartqqLogin;
import han.util.PathTool;
import org.apache.log4j.PropertyConfigurator;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author: Han
 * @date: 2018/7/11 10:05
 * @dscription: 程序入口
 */
public class Start {
    private static Logger logger = LoggerFactory.getLogger(Start.class);

    static {
        //加载log4j配置文件
        String propertiesPath = PathTool.getPath() + "\\properties\\log4j.properties";
        PropertyConfigurator.configure(propertiesPath);
    }

    private final LinkedBlockingQueue<MessageEntity> messageQueue = new LinkedBlockingQueue<>();
    private GroupDetailInfo groupDetailInfo;
    private FriendListProof friendListProof;

    private Start() {
        groupDetailInfo = GroupDetailInfo.getInstance();

        friendListProof = FriendListProof.getInstance();
    }

    public static void main(String... args) {
        Start start = new Start();
        try {
            start.loginSmartqq();
        } catch (Exception e) {
            logger.info(e.toString());
            StackTraceElement[] stackTraceElements = e.getStackTrace();
            for (StackTraceElement stackTraceElement : stackTraceElements) {
                logger.error(stackTraceElement.toString());
            }
            return;
        }
        start.groupMonitor();
        start.getFriendList();
        try {
            BeautyEyeLNFHelper.launchBeautyEyeLNF();
            UIManager.put("RootPane.setupButtonVisible", false);
        } catch (Exception e) {
            logger.warn("加载皮肤包出错");
            logger.info(e.toString());
            StackTraceElement[] stackTraceElements = e.getStackTrace();
            for (StackTraceElement stackTraceElement : stackTraceElements) {
                logger.warn(stackTraceElement.toString());
            }
        }
        new MainFrame();
    }

    /**
     * 获取并设置好友列表
     */
    private void getFriendList() {

        FriendManager friendManager = new FriendManager();
        List<FriendEntity> friendList;
        try {
            friendList = friendManager.getFriendList();
        } catch (Exception e) {
            logger.error("获取好友列表失败");
            logger.info(e.toString());
            StackTraceElement[] stackTraceElements = e.getStackTrace();
            for (StackTraceElement stackTraceElement : stackTraceElements) {
                logger.error(stackTraceElement.toString());
            }
            return;
        }
        friendListProof.setFriendList(friendList);
    }

    /**
     * 登录smartqq
     *
     * @throws Exception 抛出异常
     */
    private void loginSmartqq() throws Exception {

        SmartqqLogin smartqqLogin = new SmartqqLogin();
        smartqqLogin.login();
    }

    /**
     * 分别从smartqq和QQ群官网获取群组信息,并融合
     */
    private void fuseGroupInfo() {

        List<GroupEntity> smartqqGroupList;
        try {
            GroupManager groupManager = new GroupManager();
            smartqqGroupList = groupManager.getGroupList();
        } catch (Exception e) {
            logger.error("从smartqq获取群列表失败");
            logger.info(e.toString());
            StackTraceElement[] stackTraceElements = e.getStackTrace();
            for (StackTraceElement stackTraceElement : stackTraceElements) {
                logger.error(stackTraceElement.toString());
            }
            return;
        }

        try {
            GroupWebsiteLogin login = new GroupWebsiteLogin();
            login.login();
        } catch (Exception e) {
            logger.error("登录QQ群官网失败");
            logger.info(e.toString());
            StackTraceElement[] stackTraceElements = e.getStackTrace();
            for (StackTraceElement stackTraceElement : stackTraceElements) {
                logger.error(stackTraceElement.toString());
            }
            return;
        }

        List<GroupEntity> websiteGroupList;
        try {
            GroupWebsite groupWebsite = new GroupWebsite();
            websiteGroupList = groupWebsite.getGroupList();
        } catch (Exception e) {
            logger.error("从QQ群官网获取群列表失败");
            logger.info(e.toString());
            StackTraceElement[] stackTraceElements = e.getStackTrace();
            for (StackTraceElement stackTraceElement : stackTraceElements) {
                logger.error(stackTraceElement.toString());
            }
            return;
        }

        //融合QQ群官网和smartqq获取的群组信息
        Map<String, GroupEntity> groupMap = GroupParser.fuseGroupList(websiteGroupList, smartqqGroupList);
        groupDetailInfo.setGroupMap(groupMap);
    }

    /**
     * 只开启群组监听
     */
    private void groupMonitor() {

        fuseGroupInfo();

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        //每隔一段时间发送在线请求,保持在线状态
        executorService.submit(new KeepOnline());
        //开始接收消息
        executorService.submit(new MessageListener(messageQueue));
        //处理消息队列中的消息
        executorService.submit(new MessageProcessor(messageQueue));
        //关闭线程服务
        executorService.shutdown();
    }
}
